package zuoshen.chapter1;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {

    public static void main(String[] args) {

        Cat cat = new Cat("cat");
        System.out.println(cat instanceof Cat);
        System.out.println(cat instanceof Object);

    }

    HashSet<Pet> exist = new HashSet<>();//记录仍在队列的猫狗
    Queue<Pet> all = new LinkedList<>();
    Queue<Pet> cats = new LinkedList<>();
    Queue<Pet> dogs = new LinkedList<>();

    public void add(Pet pet){
        all.offer(pet);
        exist.add(pet);
        if(pet.getType().equals("cat")) cats.offer(pet);
        else if(pet.getType().equals("dog")) dogs.offer(pet);
        else throw new RuntimeException("no such type");
    }

    public Pet pollAll(){
        Pet pet = null;
        while (!exist.contains(pet=all.poll())); //有可能all队列里还保存在已经删除的Dog或Cat
        if(pet!=null){
            exist.remove(pet);
            if(pet.getType().equals("cat")) cats.poll();
            else dogs.poll();
        }
        return pet;
    }

    public Dog pollDog(){
        Dog dog = (Dog) dogs.poll();
        if(dog!=null) exist.remove(dog);
        return dog;
    }

    public Cat pollCat(){
        Cat cat = (Cat) cats.poll();
        if(cat!=null) exist.remove(cat);
        return cat;
    }

    public boolean isEmpty(){
        return cats.isEmpty()&&dogs.isEmpty();
    }

    public boolean isDogEmpty(){
        return dogs.isEmpty();
    }

    public boolean isCatEmpty(){
        return cats.isEmpty();
    }
}

class Pet{
    private String type;
    public Pet(String type){
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

class Cat extends Pet{
    public Cat(String type){
        super(type);
    }
}

class Dog extends Pet{
    public Dog(String type){
        super(type);
    }
}
